Create a Simple Image Button


Environment: WinXP, VC++ 6.0

 

For too long, I've wanted a simple button that displayed an image. The image needs to be iconic in nature; its visual representation should reflect its functionality.

I want the button to work correctly and appear correctly at all times. There are, of course, plenty of types of image buttons available on this Web site and I've written a few of my own. However, all of them have issues that I don't want to deal with. Owner-Draw buttons are a pain to make look good in every situation. Native buttons with the BS_BITMAP style need unreasonable care in selection of the bitmap. CBitmapButtons are clumsy to use and require a level of artwork that is beyond me. Those custom buttons that do always look and work right tend to have complicated code and lead to code bloat.

I finally started using native buttons and finding a single carefully chosen character from the installed fonts on my system. I then had to create a font and assign it to the button. All the font manipulation made my code look ugly and was not keeping with the principals of OOP, so I finally got around to creating a reusable class that encapsulated all the font manipulation.

Because the original motivation for the CGlyphButton was to have graphical Edit and Delete buttons, those two are predefined and created with a single function call. The CGlyphButton interface allows you to set the font's facename, size, weight, charset, and the single character it is to display. (While any font facename may be specified, if that font is not installed on the user's system, the desired result will not be achieved. I use only fonts that come with Windows: Arial, Wingdings, and so forth).

Declare a variable of type CGlyphButon and then in OnInitDialog set it up by using one or more of the following functions.

void SetGlyph(int cGlyph);         // Specify the single char to
                                   // display
void SetFont(LOGFONT* plf);        // Use the specified LOGFONT
                                   // on the button.
void SetFont(CFont* pFont);        // Use the specified CFont.
void SetHeight(LONG lHeight);      // Specify the height of the
                                   // displayed char.
void SetWeight(LONG lWeight);      // Specify the weight - use
                                   // the FW_* constants
void SetCharSet(BYTE bCharSet);    // DEFAULT_CHARSET is the default

void SetFaceName(CString strFaceName);    // Specify the facename.
                                          // If DEFAULT_CHARSET
                                          // not used, be sure to
                                          // specify the CHARSET
                                          // via SetCharSet()

void SetGlyphEx(LOGFONT* plf, int cGlyph);
void SetGlyphEx(CFont* pFont, int cGlyph);
void SetGlyphEx(LONG lHeight, LONG lWeight, CString strFaceName, int cGlyph);

void SetButtonType(int nType);    // Set nType to one of the class
                                  // defined constants
                                  // CGlyphButton::BTN_DELETE or
                                  // CGlyphButton::BTN_EDIT
                                  // One call sets button to
                                  // predefined type.

Source Code Notes

Demo Project Note

Note the use of a generic manifest file with a resource type of "24" (in quotations) and a resource ID of 1 (IDR_MANIFEST=1). Its presence lets WinXP draw all of the controls in the WinXP style instead of the older visual style.

Example Usage in a Dialog

// MyDlg.h : header file
//
#include "GlyphButton.h"

class CMyDlg : public CDialog
{
public:
  .
  .
  .
  // Dialog Data
  //{{AFX_DATA(CMyDlg)
  enum { IDD = IDD_MY_DIALOG };
  CGlyphButton m_btnOther;
  CGlyphButton m_btnEdit;
  CGlyphButton m_btnDelete;
  .
  .
  .
};

// MyDlg.cpp : implementation file
//
BOOL CMyDlg::OnInitDialog()
{
  CDialog::OnInitDialog();

  m_btnDelete.SetButtonType(CGlyphButton::BTN_DELETE);
    // Creates a predefined DELETE button
  m_btnEdit.SetButtonType(CGlyphButton::BTN_EDIT);
    // Creates a predefined EDIT button
  m_btnOther.SetGlyphEx(-20, FW_BOLD, "Arial", 169);

  return TRUE;
}

Downloads

Download demo project - 18 Kb
Download source - 2 Kb
 

History

Date Posted: December 10, 2003